//
//  UserInfoManager.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/3/9.
//

#import "UserInfoManager.h"
#import "YGCacheManager.h"
@interface UserInfoManager ()
@property (nonatomic, strong) YGUserInfoModel *infoModel;
@property (nonatomic, assign) BOOL  login;
@property (nonatomic, strong) YYDiskCache *cache;
@property (nonatomic, strong) NSMutableArray *cacheArray;

@end

@implementation UserInfoManager

+ (instancetype)shareInstance{
    static UserInfoManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc] init];
    });
    
    return manager;
}

- (BOOL)isLogin{
    return self.login;
}

- (void)configureUserInfo:(YGUserInfoModel *)model{
    if (!model) {
        return;
    }
    self.login = YES;
    self.infoModel = model;
    NSMutableArray *array = [NSMutableArray arrayWithArray:self.cacheArray];
//    for (YGUserInfoModel *cacheModel in array) {
//        if (cacheModel.id == model.id) {
//            [self.cacheArray removeObject:cacheModel];
//        }
//    }
    [self.cacheArray insertObject:model atIndex:0];
    [[YGCacheManager shareInstance] saveLoginUserInfo:self.cacheArray];
}

- (void)autoLoginWithUserInfo:(YGUserInfoModel *)model{
    self.login = YES;
    self.infoModel = model;
}

- (YGUserInfoModel *)getUserInfo{
    return self.infoModel;
}

- (YGUserInfoType)getUserType{
    return 1;
}

- (NSArray *)getCacheInfo{
    return [[YGCacheManager shareInstance] getUserInfos];
}

- (NSMutableArray *)cacheArray{
    if (!_cacheArray) {
        _cacheArray = [NSMutableArray array];
        [_cacheArray addObjectsFromArray:[self getCacheInfo]];
    }
    return _cacheArray;
}
@end

